package com.zdx.yswl.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import com.loopj.android.image.SmartImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.zdx.yswl.R;
import com.zdx.yswl.tool.KNetTool;
import com.zdx.yswl.ui.bean.BannerBean;
import com.zdx.yswl.ui.listener.AnimateFirstDisplayListener;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Toast;

/**
 * 楼盘 主页banner
 * 
 * @author Administrator
 */

public class BuildViewPagerAdapter extends PagerAdapter {

	public interface OnClickPagerListener {
		public void onClikPager(View v);
	};

	private OnClickPagerListener mClickListener;
	private List<View> mData = new ArrayList<View>();
	private Context context;
	private ArrayList<BannerBean> dataList = new ArrayList<BannerBean>();
	
	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	protected DisplayImageOptions options;
	
	
	public BuildViewPagerAdapter(final Context context, ArrayList<BannerBean> dataList) {
		super();
		this.dataList = dataList;
		this.context = context;
		String imageUri = "drawable://" + R.drawable.aio_image_default_round; //本地图片路径
		// 默认图片
		for (int i = 0; i < dataList.size(); i++) {
			View view = LayoutInflater.from(context).inflate(R.layout.viewpager_item, null);
			SmartImageView image = (SmartImageView) view.findViewById(R.id.image);
//			image.setImageResource(R.drawable.aio_image_default_round);
			imageLoader.displayImage(imageUri, image);
			mData.add(view);
		}
		
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.aio_image_default_round)
		.showImageForEmptyUri(R.drawable.aio_image_default_round)
		.showImageOnFail(R.drawable.aio_image_fail_round)
		 .considerExifParams(true)
		.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
		.cacheInMemory(true)
		.cacheOnDisc(true)//设置下载的图片是否缓存在SD卡中  
		.considerExifParams(true)
		.imageScaleType(ImageScaleType.EXACTLY)
//		.displayer(new RoundedBitmapDisplayer(20))//设置图片圆角
		.build();
	}

	
	
	@Override
	public int getCount() {
		if (dataList.size() <= 2) {
			return dataList.size();
		}
		return Integer.MAX_VALUE;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView(mData.get(position % dataList.size()));
		container.removeView((View)object);
		// position % urls.length 取余数
	}

	@Override
	public Object instantiateItem(ViewGroup container, final int position) {

		int num = container.getChildCount();
		for (int i = 0; i < num; i++) {
			if (container.getChildAt(i).hashCode() == mData.get(position % dataList.size()).hashCode()) {
				container.removeView(mData.get(position % dataList.size()));
				break;
			}
		}
		View view = mData.get(position % dataList.size());
		SmartImageView imageView = (SmartImageView) view.findViewById(R.id.image);
		imageLoader.displayImage(dataList.get(position % dataList.size()).imgURL, imageView, options, animateFirstListener);
		view.setTag(position % dataList.size());
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mClickListener.onClikPager(v);
			}
		});

		container.addView(view);
		
		return view;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	public void setAdaperOnClickListener(OnClickPagerListener listener) {
		this.mClickListener = listener;
	}
	
	@Override  
    public int getItemPosition(Object object) {  
     return POSITION_NONE;  
    }  
}